import Vue from 'vue'
import Router from 'vue-router'
import Home from '@/page/Home'
import search from '@/page/search'
import login from '@/page/login'
import register from '@/page/register'
import phone_login from '@/page/phone_login'
import account from '@/page/account'
import rec from '@/page/rec'
import videos from '@/page/videos'
import my from '@/page/my'
import friend from '@/page/friend'
import sms from '@/page/sms'
import ranking from '@/page/ranking'
import song from '@/page/song'
import JokerXue from '@/page/JokerXue'
import play from '@/page/play'
import follow from '@/page/follow'
import news from '@/page/news'



import $storage from 'store';


/*
const context = require.context('@/pages',false,/\.vue$/);
let components = {};
context.key().forEach(function (value) {
   let comp = context(value).default;
   components[comp.name] = comp;
});*/
Vue.use(Router);

const router = new Router({
  routes: [
    {
      path: '/',
      name: 'Home',
      component: Home,
    },
    {
      path: '/search',
      name: 'search',
      component: search
    },
    {
      path: '/login',
      name: 'login',
      component: login
    },
    {
      path: '/register',
      name: 'register',
      component: register
    },
    {
      path: '/phone_login',
      name: 'phone_login',
      component: phone_login
    },
    {
      path: '/account',
      name: 'account',
      component: account
    },
   {
      path: '/rec',
      name: 'rec',
      meta:{
        login : true,
      },
      component: rec,
    },
    {
      path: '/videos',
      name: 'videos',
      component: videos,
    },
    {
      path: '/my',
      name: 'my',
      component: my,
    },
    {
      path: '/friend',
      name: 'friend',
      component: friend,
    },
    {
      path: '/sms',
      name: 'sms',
      component: sms,
    },
    {
      path: '/ranking',
      name: 'ranking',
      component: ranking,
    },
    {
      path: '/song',
      name: 'song',
      component: song,
    },
    {
      path: '/JokerXue',
      name: 'JokerXue',
      component: JokerXue,
    },
    {
      path: '/play',
      name: 'play',
      component: play,
    },
    {
      path: '/follow',
      name: 'follow',
      component: follow,
    },
    {
      path: '/news',
      name: 'news',
      component: news,
    },



  ]
});

//路由守卫【路由实例】
router.beforeEach(function (to , from , next) {

  if (to.meta.login){
    if ($storage.get('user')){
      next();
    } else {
      next('/login');
    }
    //需要登录
  } else {
    next();
  }

});

export default router;

